/*
@title: Banana runner
@description: "Banana Runner" is a strategic movement puzzle game where the player navigates through levels to reach a portal while avoiding moving knives. The player must also collect as many keys as possible, using clever maneuvers to bypass obstacles like trees.
@author: Aaron Lei
@tags: []
@addedOn: 2024-07-11
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/
const player = "p"
const knife = "k"
const wall = "w"
const portal = "e"
const key = "y"

let totalKeysCollected = 0; // Total keys collected from all levels

setLegend(
  [player, bitmap`
................
................
.......000......
.......060......
......0660......
......06660.0...
....0003630.0...
....0.0666000...
....0.01110.....
......06660.....
.....066660.....
.....06660......
......000.......
......0.0.......
.....00.00......
................`],
  [knife, bitmap`
................
................
................
................
................
................
..........0.....
.........0......
.......00.......
......0L0.......
.....0LL0.......
....0LLL0.......
....0110........
....060.........
.....0..........
................`],
  [wall, bitmap`
................
................
................
.......D.D......
.....DDDDDD.....
....DDCDDDDD....
....DDCDDCCD....
...DDDDCDDDD....
...DDDCCCCD.....
...DDCDCCCD.....
...DCCDCC.CD....
...DCD.CC..D....
....D..CC.......
.......CC.......
.......CC.......
................`],
  [portal, bitmap`
................
................
................
................
................
......7.7.7.....
.....7.7.7.7....
....7.7.7.7.7...
.....7.7.7.7....
....7.7...7.7...
.....7.7.7.7....
......7.7.7.....
.......7.7......
........7.......
................
................`],
  [key, bitmap`
................
................
................
................
................
.......6........
......6.6.......
.......6........
.......6........
......66........
.......6........
................
................
................
................
................`],
);

let level = 0
const levels = [
  map`
.ew..
.www.
p..k.
y....`,
  map`
kw.w.
..w.w
.wwww
py...
.wwww
....e`,
  map`
kw...w...
.w.w.w.w.
.w.w.wyw.
pw.wyw.w.
.w.w.w.wy
.w.w.w.w.
..yw...we`,
  map`
...e...
..y....
...k...
.......
.......
...p...`,
  map`
..e.........
............
..y...k.....
......kk....
............
............
............
..p...y..e..`,
  map`
....k
.....
.ye..
.....
p....`,
  map`
.........w..
...www...w..
.....w...y..
www.ww.ww.ew
..w...y.www.
.www.....ww.
....y.......
k.p.........`,
  map`
............
.w.w.y.www..
.w.......www
w.y..ww.w...
wew....www.w
www.y..w....
..w......w..
....wwww...w
..p...k.ww..`,
  map`
....................
....................
w..w...........w.w..
w..w.w...w...w...www
w..w..w.w.w.w..w.w.w
w..w...w...w...w.w.w
.ww.................
....................
p...................`,
];

setMap(levels[level]);
setSolids([player, wall]);

let keysCollected = 0; // Keys collected for the current level

onInput("s", () => {
  movePlayer(0, 1); // Move player down
});

onInput("w", () => {
  movePlayer(0, -1); // Move player up
});

onInput("a", () => {
  movePlayer(-1, 0); // Move player left
});

onInput("d", () => {
  movePlayer(1, 0); // Move player right
});

function movePlayer(dx, dy) {
  const playerSprite = getFirst(player);
  const newTile = getTile(playerSprite.x + dx, playerSprite.y + dy);
  
  if (newTile.some(sprite => sprite.type === key)) {
    // Collect the key for the current level
    newTile.filter(sprite => sprite.type === key).forEach(sprite => sprite.remove());
    keysCollected++;
    
    // Update the total keys collected
    totalKeysCollected++;
  }
  
  if (newTile.some(sprite => sprite.type === portal)) {
    // Teleport the player to the next level
    playerSprite.x = 0; // Reset player position
    playerSprite.y = 0;
    
    if (level < levels.length - 1) {
      level++; // Move to the next level
      setMap(levels[level]); // Load the next level
      keysCollected = 0; // Reset keys collected for the new level
    } else {
      console.log("You have reached the last level!");
      // You can add additional logic for game completion here
    }
  }
  
  if (newTile.every(sprite => sprite.type !== wall)) {
    playerSprite.x += dx;
    playerSprite.y += dy;
  }
  
  if (keysCollected === 2 && playerSprite.x === 2 && playerSprite.y === 0) {
    console.log("Congratulations! You have collected all keys and reached the destination of Level " + level + ".");
    // You can add additional win conditions or level transitions here
  }
}

addText("Total Keys Collected: ", { x: 2, y: 0, color: color`1` });

addText("", { x: 20, y: 0, color: color`1` });

afterInput(() => {
  clearText(); // Clear all existing text on the screen
  addText("Keys Collected: " + totalKeysCollected, { x: 2, y: 0, color: color`1` }); // Add new text with updated total keys collected count
const playerSprite = getFirst(player);
  const knifeSprite = getFirst(knife);

  if (knifeSprite) {
    // Knife chasing logic
    const dx = playerSprite.x - knifeSprite.x;
    const dy = playerSprite.y - knifeSprite.y;

    if (Math.abs(dx) > Math.abs(dy)) {
      knifeSprite.x += Math.sign(dx);
    } else {
      knifeSprite.y += Math.sign(dy);
    }

    // Check if the player is touched by the knife
    if (playerSprite.x === knifeSprite.x && playerSprite.y === knifeSprite.y) {
      console.log("Player touched by the knife - Level Restarting!");
      setMap(levels[level]); // Restart the level
    }
  }
});
